#!/usr/bin/env python
# OpenGL viewer	by Kristoffer Josefsson

from vj import *

from pOSC import *

from OpenGL.GL import *
from OpenGL.GLUT import	*
from OpenGL.GLU	import *

from LSystemGL import *
from shader import *

from glew import *

from polygon import	*
from polyode import *
from math import *

import sys
import time
import os

class Stuff(Scene):
	def	__init__(self):
		Scene.__init__(self,320, 240)
		self.ESCAPE	= '\033'
		self.window	= 0
		self.rtri =	0.0
		self.t0	= 0.0
		self.triRate = 15.0
		self.rt	= 0

		self.bpm = 130
		self.ls = var(False)
		
		self.num = 0
		self.zoff = 0.0
		
		self.lightPos =[
			[ 1.0, 0.0,	0.0, 0.0],
			[ 1.0, 1.0,	0.0, 0.0],
			[ 0.0, 1.0,	0.0, 0.0],
			[ 0.0, 0.0,	1.0, 0.0]
		]
		
		self.lightSpec=[
			[ 0.9,	0.0,  0.4, 1.0],
			[ 0.4,	0.7,  0.0, 1.0],
			[ 0.0,	0.7,  0.4, 1.0],
			[ 0.4,	0.0,  0.9, 1.0]
		]
		
		self.lightDiff=[
			[ 0.0,	0.4,  0.6, 1.0],
			[ 0.0,	0.6,  0.4, 1.0],
			[ 0.4,	0.6,  0.0, 1.0],
			[ 0.6,	0.4,  0.0, 1.0]
		]
		
		self.lightAmb=[0.07, 0.07, 0.07, 1.0]
		
		self.osc = OSCReciever(self.message)
		self.size = 1.0

	def	init(self):
	
		Scene.init(self)
		self.t0=time.clock()
		self.object = polyGL()
		self.idle()
		
		self.shader = MyMaterial("../shaders/sphere.glsl")

		glLineWidth(1)

		glEnable(GL_BLEND)
		glBlendFunc(GL_SRC_ALPHA,	GL_ONE_MINUS_DST_ALPHA)
		glBlendFunc(GL_ONE , GL_ONE_MINUS_DST_ALPHA)
		glEnable(GL_POLYGON_SMOOTH)
		glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST)

		self.initLighting()
		
		self.osc.start()


		
	def idle(self):
		Scene.idle(self)
		self.r1 = sin(self.rtri/10)*2-8.5
		self.r2 = self.rtri/5.0
		self.r3 = sin(self.rtri/10)*1.8-7.5
		
	#	self.s1 = 2.0*sin(0.3*self.rtri+1.0)/2.0
		self.s1 = self.size
		self.s2 = 3.0*cos(1.53+self.rtri/5.0)/20.0
		
		self.p1 = sin(self.rtri/20.0)
		self.p2 = 2*((cos(self.rtri/50.0)+1.0)/10.0)**2
		
		self.size = self.size*0.8

	def	display(self):
	
		Scene.display(self)
	#	glClearColor(1.0,1.0,1.0,1.0)
	#	glClear(GL_COLOR_BUFFER_BIT)
		
		self.object.update(self.p1)
		self.object.compute()

		self.shader.activate()

		self.loc1 = glGetUniformLocationARB(self.shader.m_program,"n")
		glUniform1fARB(self.loc1,30.0)
	
		self.loc1 = glGetUniformLocationARB(self.shader.m_program,"LightPosition")
		self.loc2 = glGetUniformLocationARB(self.shader.m_program,"SurfaceColor")		glUniform3fARB(self.loc1,1.0,1.0,1.0)		glUniform3fARB(self.loc2,0.5,0.5,0.8)	
	
		self.loc1 = glGetUniformLocationARB(self.shader.m_program,"Radius")
		self.loc2 = glGetUniformLocationARB(self.shader.m_program,"Blend")		glUniform1fARB(self.loc1,self.s1)		glUniform1fARB(self.loc2,self.s2)	

		for i in xrange(0,6):
			glPushMatrix()
			glTranslatef(0,self.zoff,-5.0)
			glRotatef(i*360.0/6.0, 0.2, 1, 0)
			glRotatef(self.r1,0.0,1.0,0.4) 
			glRotatef(self.r2,1.0,0.0,1.0)	
			glRotatef(self.r3,0.0,1.0,0.2) 

			self.object.drawCool(0.3)
			glPopMatrix()
			
		self.shader.deactivate()
		self.object.redraw = True
		for i in xrange(0,6):
			glPushMatrix()
			glTranslatef(0,-self.zoff,-5.0)
			glRotatef(i*360.0/6.0, 0, 1, 0)
			glRotatef(self.r1,0.0,1.0,0.4) 
			glRotatef(self.r2,1.0,0.0,1.0)	
			glRotatef(self.r3,0.0,1.0,0.2) 

			self.object.drawCool(self.p2)
			self.object.draw()
			glPopMatrix()
		
		glutSwapBuffers()


	def	keyboard(self, key,	x, y):

		Scene.keyboard(self, key, x,y)

		if key == '1':
			self.object	= polyGL()
			self.object.makeTorusKnot()
		if key == '2':
			self.object	= polyGL("p2.data")
		if key == '3':
			self.object	= polyGL("p3.data")
		if key == '4':
			self.object = polyODE()
		if key == '5':
			self.object = LSystemGL(uniform(0.6,2),uniform(-0.4,0.5))
		if key == 'h':
			self.object.holComp	= False
		if key == 'H':
			self.object.holComp	= True
			
		if key == 'r':
			self.object.randomize()
			self.zoff = uniform(-2.0,2.0)
			
		if key == '6':
			s.tile(0,0,2,2)
		if key == '7':
			s.tile(1,0,2,2)
		if key == '8':
			s.tile(0,1,2,2)
		if key == '9':
			s.tile(1,1,2,2)
		if key == '0':
			s.tile(0,0,1,1)		
			
		if key == 's':
			s.pause.toggle()
			self.dir = "tiles" + string.zfill(self.num,3)
			if not os.access(self.dir, os.F_OK):
				os.mkdir(self.dir)
			for i in xrange(14):
				for j in xrange(15):
					s.tile(i, 14-j, 14,15)
					s.display()
					outfile = self.dir+"/"+"t"+string.zfill(j,3)+"x"+string.zfill(i,3)+".png"
					s.save(outfile)
			self.num += 1
			s.pause.toggle()

	def message(self,message):
		self.size = self.size + 5.0
		print(message)

if __name__=="__main__":
	s=Stuff()
#	launchConsole()
	s.main(sys.argv)
	
